package cn.meye11.crm.mapper;

import cn.meye11.crm.common.DateUtils;
import cn.meye11.crm.common.RegexUtils;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.ConsumptionRecordsEntity;
import cn.meye11.crm.domain.query.ConsumptionRecordsQuery;
import org.apache.ibatis.jdbc.SQL;

/**
 * 消费记录表(ConsumptionRecords)表数据库访问层 SQL组合类
 * 
 * @author makejava
 * @since 2023-11-23 20:16:06
 */
 
public class ConsumptionRecordsProvider {

    public String list(ConsumptionRecordsQuery query) {

        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(ConsumptionRecordsEntity.Table + " con ");
        sql.WHERE(wheres(query));
        sql.ORDER_BY(query.getSort() + " " + query.getDirection());
        return sql.toString();
    }

    public String count(ConsumptionRecordsQuery query) {

        SQL sql = new SQL();
        sql.SELECT("count (id)");
        sql.FROM(ConsumptionRecordsEntity.Table + " con ");
        sql.WHERE(wheres(query));
        return sql.toString();
    }

    private String wheres(ConsumptionRecordsQuery query) {
        StringBuilder where = new StringBuilder();
        if (query != null) {
            // 不为空时，必须为逗号分割的数字才能查询
            String id = StringUtils.commaDelimitedTrimEmptyString(query.getId());
            if (StringUtils.isNotBlank(id) && RegexUtils.check(RegexUtils.Type.ONLY_NUMBER_OR_EMPTY, id)) {
                where.append("con.id in (" + id + ")").append(" and ");
            }
            if (query.getMemberId() != null) {
                where.append("con.member_id = " + query.getMemberId()).append(" and ");
            }
            if (query.getUserId() != null) {
                where.append("con.user_id = " + query.getUserId()).append(" and ");
            }
            if (query.getAmount() != null) {
                where.append("con.amount = " + query.getAmount()).append(" and ");
            }
            if (query.getGiftAmount() != null) {
                where.append("con.gift_amount = " + query.getGiftAmount()).append(" and ");
            }
            if (StringUtils.isNotBlank(query.getItems())) {
                where.append("con.items = '" + query.getItems() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getItemsLike())) {
                where.append("con.items like '%" + query.getItemsLike() + "%'").append(" and ");
            }
            String itemsIn = StringUtils.toSqlInString(query.getItemsIn());
            if (StringUtils.isNotBlank(itemsIn)) {
                where.append("con.items in (" + itemsIn + ")").append(" and ");
            }
            String itemsNotIn = StringUtils.toSqlInString(query.getItemsNotIn());
            if (StringUtils.isNotBlank(itemsNotIn)) {
                where.append("con.items not in (" + itemsNotIn + ")").append(" and ");
            }
            if (query.getConsumptionTime() != null) {
                where.append("con.consumption_time = '" + query.getConsumptionTime()).append("' and ");
            }
            if (StringUtils.isNotBlank(query.getOperators())) {
                where.append("con.operators = '" + query.getOperators() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getOperatorsLike())) {
                where.append("con.operators like '%" + query.getOperatorsLike() + "%'").append(" and ");
            }
            String operatorsIn = StringUtils.toSqlInString(query.getOperatorsIn());
            if (StringUtils.isNotBlank(operatorsIn)) {
                where.append("con.operators in (" + operatorsIn + ")").append(" and ");
            }
            String operatorsNotIn = StringUtils.toSqlInString(query.getOperatorsNotIn());
            if (StringUtils.isNotBlank(operatorsNotIn)) {
                where.append("con.operators not in (" + operatorsNotIn + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabel())) {
                where.append("con.label = '" + query.getLabel() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabelLike())) {
                where.append("con.label like '%" + query.getLabelLike() + "%'").append(" and ");
            }
            String labelIn = StringUtils.toSqlInString(query.getLabelIn());
            if (StringUtils.isNotBlank(labelIn)) {
                where.append("con.label in (" + labelIn + ")").append(" and ");
            }
            String labelNotIn = StringUtils.toSqlInString(query.getLabelNotIn());
            if (StringUtils.isNotBlank(labelNotIn)) {
                where.append("con.label not in (" + labelNotIn + ")").append(" and ");
            }
            if (query.getStart() != null) {
                where.append("con.time >= '" + DateUtils.localDateTimeToStr(query.getStart()) + "'").append(" and ");
            }
            if (query.getEnd() != null) {
                where.append("con.time <= '" + DateUtils.localDateTimeToStr(query.getEnd()) + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getFitEntryChannels())) {
                where.append("con.fit_entry_channels = '" + query.getFitEntryChannels() + "'").append(" and ");
            }
        }
        where.append("con.del='0'");
        return where.toString();
    }
}
